package org.dk.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pojo.TbGoods;
import org.dk.pojo.TbGoodsDesc;
import org.dk.pojo.TbItem;
import org.dk.pojo.TbSeller;
import org.dk.service.GoodsDescService;
import org.dk.service.GoodsService;
import org.dk.service.ItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@RequestMapping("goodsEditController")
@Controller
public class GoodsEditController {

    @Reference
    private GoodsService goodsService;
    @Reference
    private GoodsDescService goodsDescService;
    @Reference
    private ItemService itemService;

    @RequestMapping("ToGoodShow")
    public String ToGoodShow(){
        return "admin/goods_edit";
    }

    @RequestMapping("addGoods")
    @ResponseBody
    public DataResult addGoods(TbGoods goods, TbGoodsDesc goodsDesc, String itemString){
        TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        goods.setSellerId(seller.getSellerId());
        goods.setAuditStatus("0");
        List<TbItem> tbItems = JSONObject.parseArray(itemString, TbItem.class);
        try {
            goods.setIsDelete("0");
            Long goodId = goodsService.addGoods(goods);
            goodsDesc.setGoodsId(goodId);
            goodsDescService.addGoods(goodsDesc);

            itemService.addItem(tbItems,goodId,seller.getSellerId());
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }
}
